package com.zb.daoimpl;

import java.awt.image.RescaleOp;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.zb.dao.BaseDao;
import com.zb.dao.MrDao;
import com.zb.entity.Car;
import com.zb.entity.Mr;
import com.zb.entity.Msi;
import com.zb.entity.Store;

public class MrDaoImpl implements MrDao{
	private BaseDao db=new BaseDao();
	public List<Mr> getMrList(Integer index, Integer size, String carnum) {
		List<Mr> list=new ArrayList<Mr>();
		String sql=" SELECT c.id, c.`CarNum`,c.`CarName`,c.`CarAir`,mr.`FeatExp`,mr.`FeatDate`,mr.`FeatMoney`,sto.`StoreName`,mr.`Remake`\r\n" + 
				"FROM CAR AS c JOIN mr AS mr  ON c.`id`=mr.`Id` JOIN store AS sto ON mr.`FeatShop`=sto.`id`";
		if(carnum!=null&&"".equals(carnum)) {
			sql+=" and carNum like '%" + carnum + "%'";
			
		}
		sql+=" order by carnum limit ?,?";
		ResultSet rs=db.query(sql, (index-1)*size,size);
		try {
			while(rs.next()) {
				Mr m=new Mr();
				Car c=new Car();
				Store s=new Store();
				c.setId(rs.getInt(1));
				c.setCarnum(rs.getString(2));
				c.setCarname(rs.getString(3));
				c.setCarair(rs.getString(4));
				m.setFeatExp(rs.getString(5));
				m.setFeatDate(rs.getString(6));
				m.setFeatMoney(rs.getString(7));
				s.setStoreName(rs.getString(8));
				m.setRemake(rs.getString(9));
				m.setCar(c);
				m.setStore(s);
				list.add(m);
				
				
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
			db.close();
		}
		return list;
	}

	public int getMrCount(String carnum) {
		// TODO Auto-generated method stub
		String	sql="SELECT COUNT(*)FROM CAR AS c JOIN mr AS mr  ON c.`id`=mr.`Id` JOIN store AS sto ON mr.`FeatShop`=sto.`id`";
		if(carnum!=null&&"".equals(carnum)) {
			sql+=" and carNum like '%" + carnum + "%'";
			
		}
		ResultSet rs=db.query(sql);
		int count =0;
		try {
			if(rs.next()) {
				count=rs.getInt(1);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
			db.close();
		}
		return count;
	}

	public void addMr(Mr m) {
		// TODO Auto-generated method stub
		String sql="insert mr into values(null,?,?,NOW(),?,?,?)";
		db.update(sql, m.getFeatExp(),m.getMsi().getId(),m.getFeatMoney(),m.getStore().getId(),m.getRemake());
		db.close();
	}

	public void updateMr(Mr m) {
		// TODO Auto-generated method stub
		String sql="update mr set FeatExp=?,FeatName=?,FeatDate=NOW(),FeatMoney=?,Remake=? where id=?";
		db.update(sql, m.getFeatExp(),1,m.getFeatMoney(),m.getRemake(),m.getCar().getId());
		db.close();
	}

	public void delMr(int id) {
		// TODO Auto-generated method stub
		String sql="delete from mr where id=?";
		db.update(sql, id);
		db.close();
	}

	public Mr getMrById(int id) {
		// TODO Auto-generated method stub
		String sql="select *from mr where id=?";
		ResultSet rs=db.query(sql, id);
		try {
			if(rs.next()) {
				Mr m=new Mr();
				Car c=new Car();
				Store s=new Store();
				Msi mm=new Msi();
				c.setId(rs.getInt(1));
				
				m.setFeatExp(rs.getString(2));
				mm.setId(rs.getInt(3));
				m.setFeatDate(rs.getString(4));
				m.setFeatMoney(rs.getString(5));
				s.setStoreName(rs.getString(6));
				m.setRemake(rs.getString(7));
				m.setCar(c);
				m.setStore(s);
				return m;
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
			db.close();
		}
		return null;
	}

}
